package com.yx.coredata.nettySocketIo;

import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * desc:  <br>
 * date: 2018/12/27 0027
 *
 * @author Administrator
 */
@Component
@Slf4j
public class EventListennter {

    //维护每个客户端的SocketIOClient
    private Map<Long, SocketIOClient> onlineClients = new ConcurrentHashMap<>();

    @OnConnect
    public void onConnect(SocketIOClient client) {
        long userId = Long.valueOf(client.getHandshakeData().getSingleUrlParam("userId"));
        log.info("用户{}上线了, sessionId: {}", userId, client.getSessionId().toString());
        onlineClients.put(userId,client);
    }

    @OnEvent("token")
    public void onToken(SocketIOClient client, SocketIOMessage message) {
        System.err.println("get  Message is " + message.getMessage());
        client.sendEvent("newAlert", message);

    }

//    /**
//     * 收到客户端消息
//     * @param client 客户端
//     * @param message 消息
//     */
//    @OnEvent("newAlert")
//    public void onAlert(SocketIOClient client, SocketIOMessage message) {
//        //send to all users
//        Collection<List<SocketIOClient>> clientsList = clients.values();
//        for (List<SocketIOClient> list : clientsList) {
//            for (SocketIOClient socketIOClient : list) {
//                socketIOClient.sendEvent("newAlert", message);
//            }
//        }
//    }

    /**
     * 给单个人发消息
     * @param userId 客户端
     * @param message 消息
     */
    @OnEvent("newAlert")
    public void sendOne(Long userId, SocketIOMessage message) {
        //send to one
        log.info("给用户{}发消息", userId.toString());
        Set<Map.Entry<Long, SocketIOClient>> entrySet = onlineClients.entrySet();
        for (Map.Entry<Long, SocketIOClient> entry : entrySet) {
            Long key = entry.getKey();
            if(userId == key.longValue()){
                SocketIOClient socketIOClient = entry.getValue();
                socketIOClient.sendEvent("newAlert", message);
            }
        }
    }

    /**
     * 通知所有在线客户端
     */
    public void sendAllUser() {
        Set<Map.Entry<Long, SocketIOClient>> entrySet = onlineClients.entrySet();
        for (Map.Entry<Long, SocketIOClient> entry : entrySet) {
            Long key = entry.getKey();
            SocketIOClient socketIOClient = entry.getValue();
            SocketIOMessage message = new SocketIOMessage();
            message.setMessage("hi All:" + key);
            socketIOClient.sendEvent("newAlert", message);
        }
    }

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        log.info("用户{}关闭了连接, sessionId: {}", client.getSessionId().toString());
        Set<Map.Entry<Long, SocketIOClient>> entrySet = onlineClients.entrySet();
        for (Map.Entry<Long, SocketIOClient> entry : entrySet) {
            Long key = entry.getKey();
            SocketIOClient socketIOClient = entry.getValue();
            if(client == socketIOClient){
                onlineClients.remove(key);
            }
        }
        //释放连接
        client.disconnect();
    }
}
